using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace whereIsTheDonutGame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class ContentSpawner : Microsoft.Xna.Framework.DrawableGameComponent
    {
        //   private float aspectRatio;
        donutGame game;
 
        private int numberOfTrees = 90;
        private ICamera camera;
        public List<MovableBox> boxes { get; set; }
        public List<Tree> trees { get; set; }
        public List<Solid> solids { get; set; }
        public Wall wall;
        public ContentSpawner(donutGame game)
            : base(game)
        {
            boxes = new List<MovableBox>();
            trees = new List<Tree>();
            solids = new List<Solid>();

            this.game = game;
        }

        public override void Initialize()
        {
            int x, z;
            Random random = new Random();
            camera = (ICamera)Game.Services.GetService(typeof(ICamera));
           
                boxes.Add(new MovableBox(game));
                boxes[0].LoadContent(this.Game.Content);
                x = 400;
                z = 200;
                boxes[0].Position = new Vector3(x, game.terrain.Height[x, z] + 3, -z);

                boxes.Add(new MovableBox(game));
                boxes[1].LoadContent(this.Game.Content);
                x = 300;
                z = 200;
                boxes[1].Position = new Vector3(x, game.terrain.Height[x, z] + 3, -z);



            for (int i = 0; i < numberOfTrees; i++)
            {
                x = random.Next() % 311 + 50;
                z = random.Next() % 290 +200;
                if (z > 450) z -= 50;
                trees.Add(new Tree(game));
                trees[i].LoadContent(this.Game.Content);
                trees[i].Position = new Vector3(x, game.terrain.Height[x, z], -z);
            }

            wall = new Wall(game, 430, 255);

            solids.Add(wall);
            solids.Add(game.gate1);
            solids.Add(game.gate2);


            base.Initialize();

        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }


        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            GraphicsDevice.BlendState = BlendState.Opaque;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;

            foreach (MovableBox d in boxes)
            {

                foreach (ModelMesh mesh in d.Model.Meshes)
                {
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        effect.View = camera.ViewMatrix;
                        effect.Projection = camera.ProjectionMatrix;
                        effect.World = Matrix.CreateScale(3) * Matrix.CreateTranslation(d.Position);
                        effect.EnableDefaultLighting();
                    }
                    mesh.Draw();
                }
                d.UpdateBoundingBox();

            }


            foreach (Tree t in trees)
            {


                foreach (ModelMesh mesh in t.Model.Meshes)
                {
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        effect.View = camera.ViewMatrix;
                        effect.Projection = camera.ProjectionMatrix;
                        effect.World = Matrix.CreateScale(3) * Matrix.CreateTranslation(t.Position);
                        effect.EnableDefaultLighting();
                    }
                    mesh.Draw();
                }
                t.UpdateBoundingBox();

            }
            foreach (Model m in wall.models)
            {
                foreach (ModelMesh mesh in m.Meshes)
                {
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        effect.EnableDefaultLighting();
                        
                        effect.View = camera.ViewMatrix;
                        effect.Projection = camera.ProjectionMatrix;
                        effect.World = Matrix.CreateTranslation(new Vector3(wall.x, game.terrain.Height[wall.x, wall.z], -wall.z));
                    }
                    mesh.Draw();
                }
                base.Draw(gameTime);
            }
        }

    }
}
